<?php
use Velacolib\Utility\Utility;
?>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header well" data-original-title>
            <h2><i class="icon-list"></i> Category</h2>
            <a class="btn btn-primary pull-right" style=""
               href="<?php echo $this->url('admin/child', array('controller' => 'categories', 'action' => 'add')); ?>">
                <i class="icon-edit icon-white"></i>
                New Category
            </a>
        </div>
        <div style="clear: both"></div>
        <div class="box-content">
            <table class="table table-striped table-bordered "><!-- bootstrap-datatable datatable -->
                <thead>
                <tr>
                    <th>Title</th>
                    <th>Sort Order</th>
                    <th>Actions</th>

                </tr>
                </thead>
                <tbody>
                <?php
                /*root item*/
               /*
                foreach($topic_cat as $cat){
                    if($cat['cat_parent'] == 0){
                        echo $cat['title'];
                        $id = $cat['id'];
                        draw($topic_cat, $id);
                    }
                }
                */
                /*end root item*/
                function draw($topic_cat, $parent,$prefix = '' )
                {

                    $utility = new Utility();
                    foreach ($topic_cat as $value) {
                        $num = $utility->count_topic_by_cat_id($value->id);
                        if ($value->cat_parent == $parent) {
                            $id = $value->id;
                            echo  '
                        <tr>
                            <td> <a href="/admin_abc_xyz/topic?cat_id='.$value->id.'">' . $prefix.' '.$value->title .'</a> <span class="badge badge-important">'.$num[0][0] .'</span></td>
                            <td> ' . $value->sort_order . '</td>
                            <td class="center">
                                <a href="/admin_abc_xyz/categories/edit/'.$value->id.'" class="btn btn-success edit" dataId="' . $value->id . '">
                                    <i class="icon-edit icon-white"></i>
                                    Edit
                                </a>

                                <a   class="btn btn-danger del" dataId="' . $value->id . '" href="#myModal" role="button" data-toggle="modal" >
                                    <i class="icon-trash icon-white"></i>
                                    Delete
                                </a>
                            </td>
                        </tr>
                                ';
                            draw($topic_cat,$id,$prefix.'--');

                        }

                    }


                }
                // convert object to array
                $arr = array();
                foreach($topic_cat as $topic){
                    $arr[] = $topic;
                }
                // fetching cat_parent
                $i = 0;
                foreach($arr as $a ){
                    $i++;
                    if($i ==1){
                        $id = $a->cat_parent;
                    }

                }

                //action
              draw($arr,$id);

                ?>
                <input type="hidden" name="url" id="url"
                       value="<?php echo $this->url('admin/child', array('controller' => 'categories', 'action' => 'delete')) ?>"/>

                </tbody>
            </table>
        </div>
    </div>
    <!--/span-->

</div><!--/row-->

<!-- modal -->
<div class="modal hide fade" id="myModal">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Delete option</h3>
    </div>
    <form action='<?php echo $this->url('admin/child', array('controller' => 'categories', 'action' => 'delete')) ?>'
          method="post">
        <div class="modal-body">
            <p>Please choose parent category for subcategory before delete...</p>

            <p>If you don't choose, all it will deleted..</p>

            <div class="control-group">
                <div class="controls">
                    <select id="selectError3" name="cat_parent">
                        <option value="0">Select Parent category</option>
                        <?php foreach ($category_option as $itemCate): ?>
                            <option id="<?php echo $itemCate->id; ?>" value="<?php echo $itemCate->id; ?>"><?php echo $itemCate->title ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <a id="close" href="#" class="btn" data-dismiss="modal">Close</a>
            <input type="hidden" id="del_id" name="del_id" value="">
            <button type="submit" name="submit" class="btn btn-primary">Save changes</button>
        </div>
    </form>
</div>
<!--paginator -->
<div class="row-fluid">
    <?php
    // add at the end of the file after the table
    echo $this->paginationControl(
    // the paginator object
        $topic_cat,
        // the scrolling style; see http://zf2.readthedocs.org/en/release-2.1.4/modules/zend.paginator.usage.html#rendering-pages-with-view-scripts
        'sliding',
        // the partial to use to render the control
        array('partial/paginator.phtml', 'Categories'),
        // the route to link to when a user clicks a control link
        array(
            'route' => 'admin/child',
            'child_route' => array(
                'controller'=>'Categories',
                'action' => 'index',
            )
        )
    );
    ?>
</div>
<!--end paginator -->


<script>
    jQuery(document).ready(function () {

        /* hide option */
        jQuery("a.del").click(function () {
            var delid = jQuery(this).attr("dataId");
            jQuery("#myModal").fadeIn();
            jQuery("#del_id").val(delid);
            jQuery("option#"+delid).hide();
            //window.location.href='/admin/categories/delete/'+delid;
        });

        /* */

        /* show al option */
        jQuery("a#close").click(function(){
            jQuery("option").show();
        });
    })
</script>